package com.oriental.manage.controller.institution;

import com.oriental.check.commons.util.DateUtil;
import com.oriental.manage.core.dfsUtils.FastDFSPoolUtil;
import com.oriental.manage.core.fileUtils.FileUtilsExt;
import com.oriental.manage.core.paging.Pagination;
import com.oriental.manage.core.response.ResponseDTO;
import com.oriental.manage.pojo.base.DfsFileInfo;
import com.oriental.manage.service.base.IDfsFileInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.util.Arrays;
import java.util.List;

/**
 * 支付机构对账文件管理
 *
 * @author wuxg
 * @version 1.0.0 @createTime: 2016/7/4 10:17
 */
@Slf4j
@Controller
@RequestMapping("/orgCheckFile")
public class OrgCheckFileController {

    @Autowired
    private IDfsFileInfoService iDfsFileInfoService;

    @Value("#{cfgProperties['downloadTempDir']}")
    private String downloadTempDir;

    @Autowired
    private FastDFSPoolUtil fastDFSPoolUtil;

    @RequestMapping("/init")
    public String init() {
        return "institution/searchOrgCheckFile";
    }

    @RequestMapping("/batch")
    public String batch() {
        return "institution/searchLotTradingFile";
    }

    @RequestMapping("/toAdd")
    @RequiresPermissions("orgCheckFile_update")
    public String toAdd() {
        return "institution/uploadOrgCheckFile";
    }

    @RequestMapping("/add")
    @RequiresPermissions("orgCheckFile_update")
    @ResponseBody
    public ResponseDTO<String> add(@RequestBody DfsFileInfo dfsFileInfo) {
        ResponseDTO<String> responseDTO = new ResponseDTO<>();
        try {
            dfsFileInfo.setBusiType("DZ");
            dfsFileInfo.setCompanyType("02");
            if (iDfsFileInfoService.update(dfsFileInfo) > 0) {
                responseDTO.setSuccess(true);
                responseDTO.setMsg("上传成功！");
            } else {
                responseDTO.setSuccess(false);
                responseDTO.setMsg("上传失败！");
            }
        } catch (Exception e) {
            log.error("文件上传失败:", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg(e.getMessage());
        }
        return responseDTO;
    }

    @RequestMapping("/search")
    @RequiresPermissions("orgCheckFile_search")
    @ResponseBody
    public ResponseDTO<Pagination<DfsFileInfo>> query(@RequestBody Pagination<DfsFileInfo> pagination) {
        ResponseDTO<Pagination<DfsFileInfo>> responseDTO = new ResponseDTO<>();
        try {
            pagination.getQueryBean().setPageNum(pagination.getPageNum());
            pagination.getQueryBean().setPageSize(pagination.getPageSize());
            DfsFileInfo dfsFileInfo = pagination.getQueryBean();
            dfsFileInfo.setBusiType("DZ");
            iDfsFileInfoService.searchDfsFileInfo1(pagination, dfsFileInfo);
            responseDTO.setObject(pagination);
            responseDTO.setSuccess(true);
        } catch (Exception e) {
            log.error("查询失败:", e);
            responseDTO.setSuccess(false);
            responseDTO.setMsg(e.getMessage());
        }
        return responseDTO;
    }

    @RequiresPermissions("orgCheckFile-batch_search")
    @RequestMapping("/searchBatch")
    @ResponseBody
    public ResponseDTO<Pagination<DfsFileInfo>> searchBatch(@RequestBody Pagination<DfsFileInfo> pagination) {
        ResponseDTO<Pagination<DfsFileInfo>> responseDTO = new ResponseDTO<>();
        try {
            pagination.getQueryBean().setPageNum(pagination.getPageNum());
            pagination.getQueryBean().setPageSize(pagination.getPageSize());
            DfsFileInfo dfsFileInfo = pagination.getQueryBean();
            iDfsFileInfoService.searchDfsFileInfo1(pagination, dfsFileInfo);
            responseDTO.setObject(pagination);
            responseDTO.setSuccess(true);
        } catch (Exception e) {
            log.error("查询失败:", e);
            responseDTO.setMsg(e.getMessage());
            responseDTO.setSuccess(false);
        }
        return responseDTO;
    }

    @RequestMapping("/batchDownload")
    public ResponseEntity<byte[]> batchDownload(String sBankSettleDate, String sLastUpdTime, String busiType) {
        try {
            log.info("对账文件批量下载,请求参数,银行清算日期【{}】上传日期【{}】业务类型【{}】",
                    sBankSettleDate, sLastUpdTime, busiType);
            DfsFileInfo queryBean = new DfsFileInfo();
            queryBean.setPageNum(1);
            queryBean.setPageSize(100000);
            queryBean.setBankSettleDate(DateUtil.parse(sBankSettleDate));
            queryBean.setLastUpdTime(DateUtil.parse(sLastUpdTime));
            queryBean.setBusiType(busiType);
            Pagination<DfsFileInfo> pagination = new Pagination<>();
            pagination.setQueryBean(queryBean);
            iDfsFileInfoService.searchDfsFileInfo1(pagination, queryBean);
            List<DfsFileInfo> list = pagination.getList();
            String path = downloadTempDir + "/batchFile-"
                    + (StringUtils.isBlank(sBankSettleDate) ? sLastUpdTime : sBankSettleDate) + "/";
            FileUtilsExt.writeFile(path);
            if (list != null && !list.isEmpty()) {
                for (DfsFileInfo dfsFileInfo : list) {
                    fastDFSPoolUtil.download(dfsFileInfo.getDfsGroupname(),
                            dfsFileInfo.getDfsFullFilename(),
                            path + dfsFileInfo.getLocalFilename());
                }
            }
            String zipFileName = "batchFile-"
                    + (StringUtils.isBlank(sBankSettleDate) ? sLastUpdTime : sBankSettleDate) + ".zip";
            String localFileName = downloadTempDir + "/" + zipFileName;
            File[] files = new File(path).listFiles();
            if (files != null) {
                FileUtilsExt.zipFile(Arrays.asList(files), localFileName);
            }
            FileUtilsExt.deleteDirectory(new File(path));
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attachment", new String(zipFileName.getBytes("UTF-8"), "ISO-8859-1"));
            return new ResponseEntity<>(FileUtils.readFileToByteArray(new File(localFileName)), headers, HttpStatus.CREATED);
        } catch (Exception e) {
            log.error("下载失败：", e);
        }
        return null;
    }
}
